package com.kwum.phantomjs;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * Created by mpf on 2018/1/4.
 */
public class TouTiaoUtils {

    private static TouTiaoUtils touTiaoUtils = new TouTiaoUtils();

    private TouTiaoUtils(){

    }
    public static TouTiaoUtils getTouTiaoUtilsInstance(){
        return touTiaoUtils;
    }

    public PhantomJSDriver getDriver() {
        PhantomJSDriver driver =null;

        try {
            String path ="C:/phantomjs.exe";


            DesiredCapabilities dcaps = new DesiredCapabilities();
            //ssl证书支持
            dcaps.setCapability("acceptSslCerts", true);
            //截屏支持
            dcaps.setCapability("takesScreenshot", true);
            //css搜索支持
            dcaps.setCapability("cssSelectorsEnabled", true);

            //设置代理或者其他参数
//        ArrayList<String> cliArgsCap = new ArrayList<String>();
//        cliArgsCap.add("--proxy=https://103.244.252.242:3128");
//        cliArgsCap.add("--proxy-type=https");
//        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);

            //js支持
            dcaps.setJavascriptEnabled(true);
            dcaps.setBrowserName("Chrome");
            dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, path);

            //创建无界面浏览器对象
            driver = new PhantomJSDriver(dcaps);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return driver;
    }

}
